package org.itboys.mobile.service.mongo.web;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.admin.service.lasted.SystemDictionaryService;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.encryption.Digests;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayMemberRecord;
import org.itboys.mobile.entity.mongo.SundayRecordPermission;
import org.itboys.mobile.service.mysql.web.product.SundayProductRecordService;
import org.itboys.mobile.util.NameUtil;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/10/25
 * 描述：雄安加盟网_用户记录
 */
@Service
public class SundayMemberRecordService extends BaseMongoService<SundayMemberRecord>{
    @Autowired
    private SystemDictionaryService dictionaryService;
    @Resource(name = "mobileDs")
    private MongoDataSource mobileDataSource;
    @Autowired
    private SundayUploadService uploadService;
    @Autowired
    private SundayRecordPermissionService recordPermissionService;
    @Autowired
    private SundayProductRecordService productRecordService;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mobileDataSource;
    }

    @Override
    protected Class<SundayMemberRecord> getEntityClass() {
        return SundayMemberRecord.class;
    }

    /**
     * 分页获取用户信息
     * @param request
     * @return
     */
    public PageResult<SundayMemberRecord> selectMember(HttpServletRequest request){
        Map<String,Object> param = ParamUtil.packageMongoExactParam(request,"userType","status","parentId");
        param.put("isDeleted", 0);
        Map<String,String> containsparam = ParamUtil.packageMongoVagueParam(request,"nickName","mobileName","mobile");
        PageResult<SundayMemberRecord> pageResult=super.containpageQuery(request,containsparam,param);

        return pageResult;
    }
    /**
     * 修改或新增
     * @param member
     */
    public void saveMember(SundayMemberRecord member, MultipartFile imageFile, MultipartFile backImageFile) throws  Exception{
        Map<String,Object> param =new HashMap<String, Object>();
        if(member.getId()==0l){

            param.put("isDeleted",0);
            param.put("mobile",member.getMobile());
            if(ListUtil.isNotNull(super.list(param))){
                throw new ErrorException("手机号不能重复");
            }
            //生成默认头像
            Map<String, String> map = uploadService.uploadSingle(imageFile);
            if(StringUtils.isNotEmpty(map.get(uploadService.UPLOAD_HALF_PATH))){
                member.setImage(map.get(uploadService.UPLOAD_HALF_PATH));
            }else{
                member.setImage(NameUtil.getRandomLogo());
            }
            //生成背景图
            Map<String, String> backmap = uploadService.uploadSingle(backImageFile);
            if(StringUtils.isNotEmpty(backmap.get(uploadService.UPLOAD_HALF_PATH))){
                member.setBackImage(backmap.get(uploadService.UPLOAD_HALF_PATH));
            }
            if(StringUtils.isNotEmpty(member.getPassword())){
                member.setPassword(Digests.md5(member.getPassword()));
            }

            super.save(member);
        }else{
            Map<String, String> map = uploadService.uploadSingle(imageFile);
            if(StringUtils.isNotEmpty(map.get(uploadService.UPLOAD_HALF_PATH))){
                member.setImage(map.get(uploadService.UPLOAD_HALF_PATH));
            }
            //生成背景图
            Map<String, String> backmap = uploadService.uploadSingle(backImageFile);
            if(StringUtils.isNotEmpty(backmap.get(uploadService.UPLOAD_HALF_PATH))){
                member.setBackImage(backmap.get(uploadService.UPLOAD_HALF_PATH));
            }
            param.put("id !=",member.getId());
            param.put("isDeleted",0);
            param.put("mobile",member.getMobile());
            if(ListUtil.isNotNull(super.list(param))){
                throw new ErrorException("手机号不能重复");
            }
            if(StringUtils.isNotEmpty(member.getPassword())){
                member.setPassword(Digests.md5(member.getPassword()));
            }
            super.updateExceptEmpty(member.getId(),member);
        }
    }
    /**
     *  单个用户查询
     * @param id
     * @param userType
     * @return
     */
    public SundayMemberRecord findOne(Long id,Integer userType) {
        SundayMemberRecord member =null;
        if(LongUtil.isNotZreo(id)){
            member=super.getById(id);
        }else{
            member=new SundayMemberRecord();
            member.setId(0);
            member.setUserType(userType);
        }
        member.setPassword(null);
        return member;
    }

    /**
     * 删除用户和产品
     * @param memberRecordId
     */
    public void deleteMemberRecordAndProduct(Long memberRecordId){
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("memberRecordId",memberRecordId);
        List<SundayRecordPermission> permissions=recordPermissionService.list(param);
        for (SundayRecordPermission recordPermission :permissions) {
            productRecordService.delete(recordPermission.getOtherId());
        }
        super.delete(memberRecordId);
        recordPermissionService.delete(param);
    }


}
